package config

import (
	"github.com/spf13/viper"
)

type DBConfig struct {
	DriverName string
	DBName     string
	Host       string
	Port       string
	User       string
	Password   string
}

func GetDBConfig() *DBConfig {
	config := viper.New()

	config.AddConfigPath(".")                 //设置读取的文件路径
	config.SetConfigName("config/config.yml") //设置读取的文件名
	config.SetConfigType("yaml")              //设置文件的类型
	//尝试进行配置读取
	if err := config.ReadInConfig(); err != nil {
		panic(err)
	}

	//打印文件读取出来的内容:
	return &DBConfig{
		DriverName: config.GetString("database.drivername"),
		DBName:     config.GetString("database.dbname"),
		Host:       config.GetString("database.host"),
		Port:       config.GetString("database.port"),
		User:       config.GetString("database.user"),
		Password:   config.GetString("database.password"),
	}
}
